import 'package:flutter/material.dart';
import 'package:loadmore/KProgress.dart';

class ProgressWidget extends StatefulWidget {
  @override
  _ProgressWidgetState createState() => new _ProgressWidgetState();
}

class _ProgressWidgetState extends State<ProgressWidget> with TickerProviderStateMixin {
  Animation<double> _value;

  AnimationController _positionController;

  var _isLoading = false;

  @override
  void initState() {
    super.initState();
    _positionController = new AnimationController(vsync: this, duration: new Duration(milliseconds: 1));
    _value = new Tween<double>(
      // The "value" of the circular progress indicator during a drag.
      begin: 0.0,
      end: 1.0,
    ).animate(_positionController);
  }

  @override
  void dispose() {
    super.dispose();
    _positionController.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      body: new ListView(
        children: <Widget>[
          new InkWell(
            onTap: _pressed,
            child: new Text('刷新'),
          ),
          new FadeTransition(
            child: new KProgressWidget(
              isLoading: true,
              text: "",
            ),
            opacity: _value,
          ),
        ],
      ),
    );
  }

  void _pressed() {
    _isLoading = !_isLoading;
    if (_isLoading) {
      _positionController.reverse();
    } else {
      _positionController.forward();
    }
  }
}
